<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

class KetnoiViewEngine extends JViewLegacy
{

	public function display($tpl = null) 
	{		
		$this->canDo = Utility::getActions("engine");
		if(!Utility::checkAccess("core.manage", $this->canDo))
		{
			echo Utility::showError();
			return;
		}
		
		$layout = $this->getLayout();
		if($layout == "create")
		{
			$this->id = JRequest::getInt("id",0);
			$action = empty($this->id)?"core.create":"core.edit";
			if(!Utility::checkAccess($action, $this->canDo))
			{
				echo Utility::showError();
				return;
			}
		}
		
		$layout = ucfirst($layout);
		
		if(method_exists($this, "layout$layout"))
		{
			$function = "layout$layout";
			$this->$function();
		}

		parent::display($tpl);
		
	}
	
	protected function layoutDefault()
	{
		if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
		require_once(JPATH_COMPONENT.DS.'helpers'.DS.'kendo_table.php');
		if(kendo_table::dataBinding())
		{
			$model =& $this->getModel();
            $property = kendo_table::getProperty();   
            $limitstart = $property->limitstart;
			$limit = $property->limit;
			$where = $property->where;
            if(empty($where))
			{
				$where .= " e.is_delete = 0";
			}else $where .= " and e.is_delete = 0";
            $rows = $model->loadGridData($limitstart,$limit,$where);
            $total = $model->loadAllGridData($where);
            
            $result = new stdClass();
			$result->results = $rows;
			$result->total = $total;
			echo json_encode($result);
            exit();
		}
		
	}
	
	protected function layoutCreate()
	{	
		
		$this->id = JRequest::getInt("id",0);
		$model =& $this->getModel();
		if($this->id)
		{
			
			$this->row = $model->loadData($this->id);
		}
		$model_donvi =& JModelLegacy::getInstance("Donvi", "KetnoiModel");
		$this->donvi = $model_donvi->getAllDonvi();
		$this->vendor = $model->loadAllVendor();
	}
 
}
